﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LibraryO2S;
using System.Data;
public partial class Administrator_NewsEdit : System.Web.UI.Page
{


    BizNews news = new BizNews(); 
    private BusinessController objBus = new BusinessController();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadToCombobox();
            BindData();
        }
    }
    public string NEWSID
    {
        get
        {
            return Request.QueryString["NEWSID"].ToString();
        }
    }
    public void BindData()
    {
        DataTable dataTable = news.GetNewsByID(NEWSID);

        txtTitle.Text = dataTable.Rows[0]["TITLE"].ToString();
        //txtContent.Text = dataTable.Rows[0]["IMAGE"].ToString();
        txtSumary.Text = dataTable.Rows[0]["SUMMARY"].ToString();

        ddlModule.SelectedValue = dataTable.Rows[0]["MODULEID"].ToString();
        txtSortDisplay.Text = dataTable.Rows[0]["SORTDISPLAY"].ToString();
        //txtContent.Text = dataTable.Rows[0]["CONTENT"].ToString();
        txtContent.Text = dataTable.Rows[0]["CONTENT"].ToString();
        txtKeyWord.Text = dataTable.Rows[0]["KEYWORD"].ToString();
        txtSEOdes.Text = dataTable.Rows[0]["DESCRIPTION"].ToString();
        imgDescription.ImageUrl = "~/ImageNews/" + dataTable.Rows[0]["IMAGE"].ToString();
        if (dataTable.Rows[0]["STATUS"].ToString().Trim() == "1")
        {
            cbStatus.Checked = true;
        }
        else
        {
            cbStatus.Checked = false;
        }
    }

    public void LoadToCombobox()
    {
        BizModule module = new BizModule();
        ddlModule.DataSource = module.SelectAll();
        ddlModule.DataTextField = "DESCRIPTION";
        ddlModule.DataValueField = "MODULEID";
        ddlModule.DataBind();
    }
    public string Random()
    {
        Random t = new System.Random();
        int a = t.Next();
        return a.ToString();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string filename = "";
        if (fuImage.HasFile)
        {
            filename = Random() + " " + fuImage.FileName;
            fuImage.SaveAs(Server.MapPath("~/ImageNews/") + filename);
        }
        else
        {
            DataTable dataTable = news.GetNewsByID(NEWSID);
            filename = dataTable.Rows[0]["IMAGE"].ToString();
        }
        int status = 0;
        if (cbStatus.Checked)
        {
            status = 1;       
        }
        else
            status = 0;
        try
        {
            string createdby = Session["USERNAME"].ToString();
            news.tblNewsUpdate(int.Parse(NEWSID), txtTitle.Text, filename, txtSumary.Text, int.Parse(ddlModule.SelectedValue), int.Parse(txtSortDisplay.Text), txtContent.Text, txtSEOdes.Text, txtKeyWord.Text, DateTime.Now, createdby, status);
            objBus.ResizeImage(filename, "ImageNews", "imgnews", 557, 285);
            objBus.ResizeImage(filename, "ImageNews", "imgnewscrop", 183, 122);
            
            divMessage.InnerHtml = O2S_Message.Success("Hệ thống", "Thực hiện thành công");
            txtTitle.Text = "";
            txtSumary.Text = "";
            txtContent.Text = "";
            cbStatus.Checked = false;


        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = O2S_Message.Error("Hệ thống", "Phát hiện lỗi" + ex.Message);
        }

    }
}